package com.example.livemall.mapper;

import com.example.livemall.entity.ValidateCode;
import com.example.livemall.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import org.springframework.stereotype.Repository;

import java.time.Duration;

/**
 * 验证码服务数据层
 */
@Repository
public class SendyzmDaoImpl implements SendyzmMapper {

    @Autowired
    private RedisCache redisCache;

    @Value("${origin}")
    private String origin;

    /**
     * 保存验证码
     *
     * @param phone
     * @param validateCode
     */
    @Override
    public void setCode(String phone, ValidateCode validateCode) {
       redisCache.setCacheObject(origin+ phone, validateCode, Duration.ofMinutes(2L));
    }

    /**
     * 获取验证码
     *
     * @param phone
     * @return
     */
    @Override
    public ValidateCode getCode(String phone) {
        return redisCache.getCacheObject(origin + phone);
    }

    /**
     * 删除验证码
     *
     * @param phone
     */
    @Override
    public boolean deleteCode(String phone) {
        return redisCache.deleteObject(origin + phone);
    }

}